package com.feituan.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.feituan.core.api.PrinterClient;
import com.feituan.core.pojo.api.AuthorizationReq;
import com.feituan.core.pojo.api.PrinterReq;
import com.feituan.core.pojo.request.PrinterRequest;
import com.feituan.core.redis.cache.RedisCache;
import com.feituan.core.service.IEquipmentShopService;
import com.feituan.core.service.IPrinterService;
import com.feituan.core.util.MdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PrinterServiceImpl implements IPrinterService {
    @Autowired
    private PrinterClient printerClient;

    @Autowired
    private MdUtils mdUtils;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IEquipmentShopService equipmentShopService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void authorization() {
        AuthorizationReq authorizationReq = new AuthorizationReq();
        authorizationReq.setClient_id("1057678567");
        long authorTime = System.currentTimeMillis() / 1000;
        authorizationReq.setTimestamp(Math.toIntExact(authorTime));
        String authorSign = authorizationReq.getClient_id() + authorTime + "8b3936e7baa6344a524f86ef646af943";
        String s = mdUtils.md5(authorSign);
        authorizationReq.setSign(s);
        authorizationReq.setId(UUID.randomUUID().toString());
        authorizationReq.setGrant_type("client_credentials");
        authorizationReq.setScope("all");
        JSONObject jsonObject = printerClient.getAuthorization(authorizationReq);
        JSONObject bodyObject = jsonObject.getJSONObject("body");
        String accessToken = bodyObject.getString("access_token");
        redisCache.setCacheObject("accessToken",accessToken);
    }
    /**
     * 打印
     */
    @Override
    public boolean printer(List<PrinterRequest> list) {
        try {
            for (PrinterRequest reqList : list) {
                PrinterReq printerReq = new PrinterReq();
                printerReq.setClient_id("1057678567");
                long printTime = System.currentTimeMillis() / 1000;
                String printSign = printerReq.getClient_id() + printTime + "8b3936e7baa6344a524f86ef646af943";
                String s1 = mdUtils.md5(printSign);
                printerReq.setSign(s1);
                printerReq.setTimestamp(Math.toIntExact(printTime));
                if (redisCache.getCacheObject("accessToken") == null) {
                    authorization();
                }
                String accessToken = redisCache.getCacheObject("accessToken");
                printerReq.setAccess_token(accessToken);
                printerReq.setId(UUID.randomUUID().toString());
                printerReq.setMachine_code(reqList.getEquipmentCode());
                printerReq.setOrigin_id(UUID.randomUUID().toString());
                printerReq.setContent(reqList.getTemplate());
                printerClient.printer(printerReq);
            }
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
